package com.yc.navy.dayfresh.service.Impl;

import java.com.yc.navy.dayfresh.exception.ProductStoreException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yc.navy.dayfresh.bean.OrderItemInfo;
import com.yc.navy.dayfresh.dto.OrderDetailInfoDTO;
import com.yc.navy.dayfresh.dto.OrderInfoDTO;
import com.yc.navy.dayfresh.dto.ProductInfoDTO;
import com.yc.navy.dayfresh.dto.SalesInfoDTO;
import com.yc.navy.dayfresh.mapper.IOrderInfoMapper;
import com.yc.navy.dayfresh.mapper.IOrderItemInfoMapper;
import com.yc.navy.dayfresh.mapper.IProductInfoMapper;
import com.yc.navy.dayfresh.service.IOrderInfoService;


@Service
public class OrderInfoServiceImpl implements IOrderInfoService{
	@Autowired
	private IOrderInfoMapper orderInfoMapper;
	
	@Autowired
	private IOrderItemInfoMapper orderItemInfoMapper;
	
	@Autowired
	private IProductInfoMapper productInfoMapper;
	
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	@Transactional 
	@Override
	public Map<String, Object> add(String[] pnos, String ano, Long mno) {
		System.out.println(Arrays.toString(pnos) + "\t" + ano + "\t" + mno);
		String ono = UUID.randomUUID().toString();
		
		List<ProductInfoDTO> products = productInfoMapper.findByPnos(pnos);
		
		List<OrderItemInfo> orderItems = new ArrayList<OrderItemInfo>();
		OrderItemInfo orderItem = null;
		
		List<SalesInfoDTO> sales = new ArrayList<SalesInfoDTO>();
		SalesInfoDTO sale = null;
		
		double totalPrice = 0;
		int num = 0;
		Map<Object, Object> map = redisTemplate.opsForHash().entries("m_" + mno);
		for(ProductInfoDTO pf: products) {
			num = Integer.parseInt(String.valueOf(map.get("p_" + pf.getPno())));//获取购买数量
			if(pf.getBalance() < num) {
//				throw new ProductStoreException(pf.getPname());
			}
			
			totalPrice +=num * pf.getPrice();
			
			orderItem = new OrderItemInfo();
			BeanUtils.copyProperties(pf, orderItem);
			orderItem.setOno(ono);
			orderItem.setNums(num);
			orderItems.add(orderItem);
			
			sale = new SalesInfoDTO(pf.getPno(), num);
			sales.add(sale);
		}
		
		//添加订单 OrderInfoDTO
		OrderInfoDTO orderInfo = new OrderInfoDTO();
		orderInfo.setAno(ano);
		orderInfo.setMno(mno);
		orderInfo.setOno(ono);
		orderInfo.setTotalPrice(totalPrice);
		
		int result = orderInfoMapper.add(orderInfo);
		
		//添加订单详细list<OrderItemInfo>
		result = orderItemInfoMapper.add(orderItems);
		
		//扣存款SalesInfoDTO
		result = productInfoMapper.updateStore(sales);//？？？？
		
		Map<String, Object> resultMap = new HashMap<String, Object> ();
		resultMap.put("ono", ono);
		resultMap.put("totalPrice", totalPrice);
		
		if(result > 0) {
			//删购物车
			for(String pno: pnos) {
				redisTemplate.opsForHash().delete("m_" + mno, "p_" + pno);
			}
			return resultMap;
		}
		return Collections.emptyMap();
	}

	@Override
	public int update(Map<String, Object> map) {
		return orderInfoMapper.update(map);
	}

	@Override
	public List<OrderDetailInfoDTO> finds(Map<String, Object> map) {
		return orderInfoMapper.finds(map);

	}

}
